package com.fuhao.productservice.controller;

import com.fuhao.productservice.model.Product;
import com.fuhao.productservice.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/product")
@RefreshScope
public class ProductController {

    @Value("${server.port}")
    private String port;
    //测试通过消息总线动态拉去刷新配置数据
    @Value("${env}")
    private String env;
    @Autowired
    private ProductService productService;

    @RequestMapping("/list")
    public List<Product> listProduct(){
        return productService.listProduct();
    }

    @RequestMapping("/findById")
    public Product findById(Integer id){
        Product product = productService.findById(id);
        Product result = new Product();
        BeanUtils.copyProperties(product, result);
        result.setName(result.getName()+"  port="+port);
        System.out.println("测试动态刷新配置，env="+env);
        return result;
    }

}
